package com.gewu.bookeeping.View.ItemFragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.gewu.bookeeping.MyDb.bean.MesBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.MesInPerson;
import com.gewu.bookeeping.OnClick.goBack;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.View.activity.ItemActivity;

import java.util.List;

public class twoItem2Fragment extends Fragment {

    private List<MesBeanIn> quer4;
    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_two_item2_fragment, container, false);
        initview(inflate);
        return inflate;
    }

    private void initview(View inflate) {
        ListView listView = inflate.findViewById(R.id.listview);
        MesInPerson mesInPerson = new MesInPerson(getContext());
        quer4 = mesInPerson.quer();

        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return quer4 == null ? 0 : quer4.size();
        }

        @Override
        public Object getItem(int position) {
            return quer4.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, final ViewGroup parent) {
            @SuppressLint("ViewHolder")
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item_content, parent, false);
            TextView name = inflate.findViewById(R.id.name);
            TextView time = inflate.findViewById(R.id.time);
            TextView type = inflate.findViewById(R.id.type);
            LinearLayout tab = inflate.findViewById(R.id.tab);
            name.setText(quer4.get(position).getText());
            time.setText(quer4.get(position).getTime());
            type.setText(quer4.get(position).getType());
            tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(parent.getContext(), ItemActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("values", 1);
                    bundle.putInt("type", 1);
                    intent.putExtra("date", bundle);
                    parent.getContext().startActivity(intent);
                }
            });
            return inflate;
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            myAdapter.notifyDataSetChanged();
        }
    }
}